unit dkClasses;

interface

uses
  Classes, SysUtils, windows, Contnrs;

type
  TdkStringList = class(TStringList)
  private
    function GetNameFromValue(V: string): string;
    function GetIndexFromValue(V: string): integer;
    function GetValueFromIndex(Index: Integer): string;
    procedure SetValueFromIndex(Index: Integer; const Value: string);
  protected
    function ExtractValue(const S: string): string;
  public
    property NameFromValue[Val: string]: string read GetNameFromValue;
    property IndexFromValue[Val: string]: Integer read GetIndexFromValue;
    property ValueFromIndex[Index: Integer]: string read GetValueFromIndex write SetValueFromIndex;
  end;


implementation

{ TdkStringList }

function TdkStringList.ExtractValue(const S: string): string;
var
  P: Integer;
begin
  Result := S;
  P := AnsiPos(NameValueSeparator, Result);
  if P <> 0 then
    result := Copy(Result, P + 1, MaxInt);
end;

function TdkStringList.GetIndexFromValue(V: string): integer;
var
  i: integer;
begin
  result := -1;
  for i := 0 to Count - 1 do
  begin
    if CompareStrings(ExtractValue(Get(i)), V) = 0 then
    begin
      result := i;
      break;
    end;
  end;
end;

function TdkStringList.GetNameFromValue(V: string): string;
begin
  result := Names[IndexFromValue[V]];
end;

function TdkStringList.GetValueFromIndex(Index: Integer): string;
begin
  if Index >= 0 then
    Result := Copy(Get(Index), Length(Names[Index]) + 2, MaxInt) else
    Result := '';
end;

procedure TdkStringList.SetValueFromIndex(Index: Integer;
  const Value: string);
begin
  if Value <> '' then
  begin
    if Index < 0 then Index := Add('');
    Put(Index, Names[Index] + NameValueSeparator + Value);
  end
  else
    if Index >= 0 then Delete(Index);
end;

end.

